package com.nl.controller.web.controller;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.ibm.icu.text.SimpleDateFormat;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.CheckMgr;
import com.nl.controller.web.bean.CheckNum;
import com.nl.controller.web.service.CheckNumService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("check")
public class CheckNumController {
	@Autowired
	private CheckNumService checkNumService;

	@ApiOperation(value = "定位号码", notes = "定位号码")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
		@ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
		@ApiImplicitParam(name = "cityId", value = "地市编码", required = false, dataType = "String"),
		@ApiImplicitParam(name = "msisdn", value = "手机号码", required = false, dataType = "String"),
		@ApiImplicitParam(name = "mgrId", value = "项目编码", required = false, dataType = "String"),
		@ApiImplicitParam(name = "mgrName", value = "项目简称", required = false, dataType = "String"),
		@ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String"),
		@ApiImplicitParam(name = "endTime", value = "截止时间", required = false, dataType = "String")
	})
	@PostMapping("num")
	public Response findNum(
			@RequestParam(value = "page", required = true) int page,
			@RequestParam(value = "limit", required = true) int limit,
			@RequestParam(value = "cityId", required = false) String cityId,
			@RequestParam(value = "msisdn", required = false) String msisdn,
			@RequestParam(value = "countyId", required = false) String countyId,
			@RequestParam(value = "mgrId", required = false) String mgrId,
			@RequestParam(value = "mgrName", required = false) String mgrBrief,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime
			) {
		if ("".equals(cityId) || cityId == null) {
			throw new SmsException("请选择查询条件，开始查询");
		}else {
			log.info(" 定位号码：{}", startTime+","+endTime+","+msisdn+",");
			if (null==startTime || "".equals(startTime) || null==endTime || "".equals(endTime)) {
				Date date = new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
				endTime = sdf.format(date); //格式化当前天
				startTime = endTime.substring(0,10)+" 00:00:00";
			}else {
				startTime = this.thinkStartTime(startTime);
				endTime = this.thinkEndTime(endTime);
			}
			PageInfo<CheckNum> pageInfo = checkNumService.checkNum(countyId,cityId, mgrId, msisdn, mgrBrief,startTime,endTime);
			log.info(" 定位号码：{}", cityId+","+mgrId+","+msisdn);
			log.debug("定位号码返回参数：{}", pageInfo.getList().toString());
			return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
		}
	}
	@PostMapping("mgr")
	public Response findMgr(
			@RequestParam(value = "page", required = true) int page,
			@RequestParam(value = "limit", required = true) int limit,
			@RequestParam(value = "cityId", required = false) String cityId,
			@RequestParam(value = "countyId", required = false) String countyId,
			@RequestParam(value = "msisdn", required = false) String msisdn,
			@RequestParam(value = "mgrId", required = false) String mgrId,
			@RequestParam(value = "mgrName", required = false) String mgrBrief,
			@RequestParam(value = "startTime", required = false) String startTime,
			@RequestParam(value = "endTime", required = false) String endTime
			) {
		if ("".equals(cityId) || cityId == null) {
			throw new SmsException("请选择查询条件，开始查询");
		}else {
			//开始或结束时间为空,默认当天
			if (null==startTime || "".equals(startTime) || null==endTime || "".equals(endTime)) {
				Date date = new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
				endTime = sdf.format(date); //格式化当前天
				startTime = endTime.substring(0,10)+" 00:00:00";
			}
			log.info(" 定位项目：{}", cityId+","+countyId+","+mgrId+","+msisdn+",");
			PageInfo<CheckMgr> pageInfo = checkNumService.checkMgr(cityId,countyId, mgrId,mgrBrief,startTime,endTime);
			log.info(" 定位项目：{}", cityId+","+countyId+","+mgrId+","+msisdn);
			log.debug("定位项目返回参数：{}", pageInfo.getList().toString());
			return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
		}
	}
	
	private String thinkStartTime(String startTime){
		
		Date date = new Date();
		Calendar calendar = Calendar.getInstance(); //得到日历
		calendar.setTime(date);//把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
		Date dBefore = calendar.getTime();  //得到前一天的时间
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
		String defaultStartDate = sdf.format(dBefore); //格式化前一天
		String now = sdf.format(date);
		String yesterday = defaultStartDate.substring(0,10)+" 00:00:00";
		if ("".equals(startTime)  || null ==startTime) {
			return yesterday;
		}
		if (yesterday.compareTo(startTime)<=0 && now.compareTo(startTime)>0) {
			return startTime;
		}else {
			return yesterday;
		}
	}
	private String thinkEndTime(String EndTime){
		Date date = new Date();
		Calendar calendar = Calendar.getInstance(); //得到日历
		calendar.setTime(date);//把当前时间赋给日历
		calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
		Date dBefore = calendar.getTime();  //得到前一天的时间
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
		String defaultStartDate = sdf.format(dBefore); //格式化前一天
		String now = sdf.format(date);
		String yesterday = defaultStartDate.substring(0,10)+" 00:00:00";
		if ("".equals(EndTime)  || null ==EndTime) {
			return now;
		}
		if(now.compareTo(EndTime)>0 && yesterday.compareTo(EndTime)<=0) {
			return EndTime;
		} else {
			return now;
		}
	}
}
